# This file is intended to be a final submission. python tester.py Player.py
# should work at all times. If it does not, there is a bug.
# If you're just trying to test a solution, scroll down to the Player
# class.

# This file is intended to be in the same format as a valid solution, so
# that users can edit their solution into Player and then submit just this
# file to the contest. If you see any reason this would not work, please submit
# an Issue to https://github.com/ChadAMiller/hungergames/issues or email me.

# You can see more sample player classes in bots.py

class BasePlayer(object):
    '''
    Base class so I don't have to repeat bookkeeping stuff.
    Do not edit unless you're working on the simulation.
    '''
    
    def __str__(self):
        try:
            return self.name
        except AttributeError:
            # Fall back on Python default
            return super(BasePlayer, self).__repr__()
    
    def hunt_choices(*args, **kwargs):
        raise NotImplementedError("You must define a strategy!")
        
    def hunt_outcomes(*args, **kwargs):
        pass
        
    def round_end(*args, **kwargs):
        pass


class Player(BasePlayer):
    '''Player that mimics the action of the other player'''
    def __init__(self):
        self.name = "MimicHunter"
        # We have a list of enemy objects, then we have a map that maps the reputation that changes every round
        # to the actual enemy it corresponds to.
        self.enemies = 0
        # from enemy index to player_reputations index, enemies and map are a mirriors
        self.map = 0
        self.enemyCount = 0
        
    def getInfo(self):
        print("*** Enemies Info *** ")
        print(" Enemy count: " + str(self.enemyCount))
        print
        for e in self.enemies:
            e.getInfo()
            print
       

    def hunt_choices(self, round_number, current_food, current_reputation, m, player_reputations):
        # the first round we get the enemy count and create an enemy object
        # for each enemy. We hunt with everyone
        if round_number == 1:
            self.enemies = [Enemy(count) for count in range(len(player_reputations))]
            self.enemyCount = count + 1
            self.map = [i for i in range(self.enemyCount)]
            return ['h' for rep in player_reputations]
        else:
            result = ['h' for rep in player_reputations]
            for idx in range(len(player_reputations)):
                rep = player_reputations[idx]
                enemy = self.findMatch(rep, round_number, self.enemyCount)
                self.map[idx] = enemy.id
            
            for e in self.enemies:
                e.reset()
            
            for idx in range(len(player_reputations)):
                self.enemies[idx].myMove = self.enemies[idx].hisMove;
                result[self.map[idx]] = self.enemies[idx].hisMove;
            
                            
            return result
        
        
    def hunt_outcomes(self, food_earnings):
        
        for i in range(len(self.map)):
            self.enemies[i].updateResult(food_earnings[self.map[i]])
            
     #   self.getInfo()
        
    def findMatch(self, rep, round_num, enemyCount):
        for idx in range(len(self.enemies)):
            if self.enemies[idx].isEnemyMatch(rep, round_num, enemyCount):
                return self.enemies[idx]
            
            
        # we didn't find a match shit, get first item assign a match
        for idx in range(len(self.enemies)):
            if self.enemies[idx].isUpdated == False:
                self.enemies[idx].isUpdated = True
                max_heads = (round_num - 1) * enemyCount
                total_heads = max_heads * rep
                self.enemies[idx].hCount = total_heads
                return self.enemies[idx]
        
    
        return False
    
    

    
class Enemy():
    def __init__(self, id):
        self.id = id
        self.reputation = 0.0
        self.myMove = 'h'
        self.hisMove = 'h'
        self.hCount = 0
        self.isUpdated = False
        
    def getInfo(self):
        print(" My id is : " + str(self.id))
        print(" My reputation is : " + str(self.reputation))
        print(" My move is : " + str(self.myMove))
        print(" My enemy move is : " + str(self.hisMove))
        print(" his h count is : " + str(self.hCount))
        
    def updateResult(self, earnings):
        # We both hunted
        if earnings == 0:
            self.hisMove = 'h'
        # We both slacked
        elif earnings == -2:
            self.hisMove = 's'
        # I hunted he slacked
        elif earnings == -3:
            self.hisMove = 's'
        # I slacked he hunted
        elif earnings == 1:
            self.hisMove = 'h'
                
    def isEnemyMatch(self, reputation, round_number, enemy_count):
        
      #  print("********* CHECKING MATCH *********")
      #  print("id : " + str(self.id))
      #  print("hcount : " + str(self.hCount))
        
        
       # print("reputation : " + str(reputation))
      #  print("round_number : " + str(round_number))
        
        if (self.isUpdated == True):
            return False;
        # Maximum of heads this enemy could possibly have
        max_heads = (round_number - 1) * enemy_count
        

        
        # The number of heads given the reputation
        total_heads = max_heads * reputation
        
    #    print("max_heads : " + str(max_heads))
      #  print("total_heads : " + str(total_heads))
        
        if (total_heads <= self.hCount + enemy_count and total_heads >= self.hCount):
            
           # print(" YES we got a match")
            self.isUpdated = True
            self.hCount = total_heads
            return True
        else:
           # print(" NO we did not get a match")
            return False
        
    def reset(self):
        self.isUpdated = False
        
